{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Incremental Memory Leak"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: memLeak"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #增长的内存泄露"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个整数 <code>memory1</code> 和 <code>memory2</code> 分别表示两个内存条剩余可用内存的位数。现在有一个程序每秒递增的速度消耗着内存。</p>\n",
    "\n",
    "<p>在第 <code>i</code> 秒（秒数从 1 开始），有 <code>i</code> 位内存被分配到 <strong>剩余内存较多</strong> 的内存条（如果两者一样多，则分配到第一个内存条）。如果两者剩余内存都不足 <code>i</code> 位，那么程序将 <b>意外退出</b> 。</p>\n",
    "\n",
    "<p>请你返回一个数组，包含<em> </em><code>[crashTime, memory1<sub>crash</sub>, memory2<sub>crash</sub>]</code> ，其中 <code>crashTime</code>是程序意外退出的时间（单位为秒），<em> </em><code>memory1<sub>crash</sub></code><em> </em>和<em> </em><code>memory2<sub>crash</sub></code><em> </em>分别是两个内存条最后剩余内存的位数。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>memory1 = 2, memory2 = 2\n",
    "<b>输出：</b>[3,1,0]\n",
    "<b>解释：</b>内存分配如下：\n",
    "- 第 1 秒，内存条 1 被占用 1 位内存。内存条 1 现在有 1 位剩余可用内存。\n",
    "- 第 2 秒，内存条 2 被占用 2 位内存。内存条 2 现在有 0 位剩余可用内存。\n",
    "- 第 3 秒，程序意外退出，两个内存条分别有 1 位和 0 位剩余可用内存。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>memory1 = 8, memory2 = 11\n",
    "<b>输出：</b>[6,0,4]\n",
    "<b>解释：</b>内存分配如下：\n",
    "- 第 1 秒，内存条 2 被占用 1 位内存，内存条 2 现在有 10 位剩余可用内存。\n",
    "- 第 2 秒，内存条 2 被占用 2 位内存，内存条 2 现在有 8 位剩余可用内存。\n",
    "- 第 3 秒，内存条 1 被占用 3 位内存，内存条 1 现在有 5 位剩余可用内存。\n",
    "- 第 4 秒，内存条 2 被占用 4 位内存，内存条 2 现在有 4 位剩余可用内存。\n",
    "- 第 5 秒，内存条 1 被占用 5 位内存，内存条 1 现在有 0 位剩余可用内存。\n",
    "- 第 6 秒，程序意外退出，两个内存条分别有 0 位和 4 位剩余可用内存。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>0 &lt;= memory1, memory2 &lt;= 2<sup>31</sup> - 1</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [incremental-memory-leak](https://leetcode.cn/problems/incremental-memory-leak/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [incremental-memory-leak](https://leetcode.cn/problems/incremental-memory-leak/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['2\\n2', '8\\n11']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        m1 = memory1\n",
    "        m2 = memory2\n",
    "        \n",
    "        val = 1\n",
    "        while max(m1, m2) >= val:\n",
    "            if m1 >= m2:\n",
    "                m1 -= val\n",
    "            else:\n",
    "                m2 -= val\n",
    "            \n",
    "            val += 1\n",
    "\n",
    "        return [val, m1, m2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        cnt = 0\n",
    "        while True:\n",
    "            cnt += 1\n",
    "            m = memory1 if memory1 >= memory2 else memory2\n",
    "            if m < cnt:\n",
    "                return [cnt, memory1, memory2]\n",
    "            if memory1 >= memory2:\n",
    "                memory1 -= cnt\n",
    "            else:\n",
    "                memory2 -= cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        time = 1\n",
    "        while max(memory1, memory2) >= time:\n",
    "            if memory2 > memory1:\n",
    "                memory2 -= time\n",
    "            else:\n",
    "                memory1 -= time\n",
    "            time += 1\n",
    "        return [time, memory1, memory2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        i = 1\n",
    "        while memory1 >= i or memory2 >= i:\n",
    "            if memory1 >= memory2:\n",
    "                memory1 = memory1 - i\n",
    "            else: memory2 = memory2 - i\n",
    "            i = i + 1\n",
    "        return [i, memory1, memory2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        m1, m2 = max(memory1, memory2), min(memory1, memory2)\n",
    "\n",
    "        n = int(math.sqrt(2 * (m1 - m2) + 1/4) - 1/2)\n",
    "        m1 -= n * (n + 1) // 2\n",
    "\n",
    "        swap = memory2 > memory1 and m1 != m2\n",
    "        \n",
    "        a = int(math.sqrt(m1 + n * n / 4) - n/2)\n",
    "        m1 -= (n + a) * a\n",
    "        m2 -= (n + a) * (a - 1)\n",
    "\n",
    "        n += 2 * a\n",
    "\n",
    "        if m2 >= n:\n",
    "            m2 -= n\n",
    "            n += 1\n",
    "        \n",
    "        if swap:   \n",
    "            m1, m2 = m2, m1\n",
    "\n",
    "        return [n, m1, m2]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "\n",
    "        m1, m2 = max(memory1, memory2), min(memory1, memory2)\n",
    "\n",
    "        n = int(math.sqrt(2 * (m1 - m2) + 1/4) - 1/2)\n",
    "        m1 -= n * (n + 1) // 2\n",
    "\n",
    "        swap = memory2 > memory1 and m1 != m2\n",
    "        \n",
    "        a = int(math.sqrt(m1 + n * n / 4) - n/2)\n",
    "        m1 -= (n + a) * a\n",
    "        m2 -= (n + a) * (a - 1)\n",
    "\n",
    "        n += 2 * a\n",
    "\n",
    "        if m2 >= n:\n",
    "            m2 -= n\n",
    "            n += 1\n",
    "        \n",
    "        if swap:   \n",
    "            m1, m2 = m2, m1\n",
    "\n",
    "        return [n, m1, m2]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, m1: int, m2: int) -> List[int]:\n",
    "        for i in range(1, 10**10):\n",
    "            if m1 >= m2:\n",
    "                if m1 < i: return [i, m1, m2]\n",
    "                m1 -= i\n",
    "            else:\n",
    "                if m2 < i: return [i, m1, m2]\n",
    "                m2 -= i\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        t = 1\n",
    "        while True:\n",
    "            if memory1 < t and memory2 < t: break\n",
    "            if memory1 >= memory2:\n",
    "                memory1 -= t\n",
    "            else:\n",
    "                memory2 -= t\n",
    "            t += 1\n",
    "        return [t, memory1, memory2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import heapq\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        start = 1\n",
    "        while max(memory1, memory2) - start >= 0:\n",
    "            if memory1 > memory2:\n",
    "                memory1 -= start\n",
    "            elif memory2 > memory1:\n",
    "                memory2 -= start\n",
    "            else:\n",
    "                memory1 -= start\n",
    "            start += 1\n",
    "        return [start, memory1, memory2]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        i = 1\n",
    "        while i <= memory1 or i <= memory2:\n",
    "            if memory1 >= memory2:\n",
    "                memory1 -= i\n",
    "            else:\n",
    "                memory2 -= i\n",
    "            i += 1\n",
    "        return [i, memory1, memory2]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        tag = 1\n",
    "        i = 1\n",
    "        while tag:\n",
    "            # 退出条件\n",
    "            if i > memory1 and i > memory2:\n",
    "                tag = 0\n",
    "                continue\n",
    "            # 判断选1还是选2\n",
    "            if memory1 >= memory2:\n",
    "                memory1 -= i\n",
    "            else:\n",
    "                memory2 -= i\n",
    "            i += 1\n",
    "        \n",
    "        return [i, memory1, memory2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        i = 1\n",
    "        while memory1 >= i or memory2 >= i:\n",
    "            if memory2 > memory1:\n",
    "                memory2 -= i\n",
    "            else:\n",
    "                memory1 -= i\n",
    "            i += 1\n",
    "        return [i , memory1 , memory2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        i = 1  # 开始的秒数\n",
    "        while True:\n",
    "            # 如果两个内存条的剩余内存都小于 i 位，则程序意外退出\n",
    "            if memory1 < i and memory2 < i:\n",
    "                return [i, memory1, memory2]\n",
    "\n",
    "            # 如果第一个内存条的剩余内存大于或等于第二个内存条的剩余内存\n",
    "            if memory1 >= memory2:\n",
    "                memory1 -= i\n",
    "            else:\n",
    "                memory2 -= i\n",
    "            i += 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        i = 1\n",
    "        while i <= memory1 or i <= memory2:\n",
    "            if memory1 >= memory2: memory1 -= i \n",
    "            else:                  memory2 -= i\n",
    "            i += 1\n",
    "        return [i, memory1, memory2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        time = 1\n",
    "        while (time <= max(memory1, memory2)):\n",
    "            if memory1 >= memory2:\n",
    "                memory1 -= time\n",
    "            else:\n",
    "                memory2 -= time \n",
    "            time += 1\n",
    "        return [time, memory1, memory2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        cnt = 1\n",
    "        while True:\n",
    "            if cnt > memory1 and cnt > memory2:\n",
    "                return [cnt, memory1, memory2] \n",
    "            if memory1 >= memory2:\n",
    "                memory1 -= cnt\n",
    "            else:\n",
    "                memory2 -= cnt\n",
    "            cnt += 1\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        m1, m2 = max(memory1, memory2), min(memory1, memory2)\n",
    "\n",
    "        n = int(math.sqrt(2 * (m1 - m2) + 1/4) - 1/2)\n",
    "        m1 -= n * (n + 1) // 2\n",
    "\n",
    "        swap = memory2 > memory1 and m1 != m2\n",
    "        \n",
    "        a = int(math.sqrt(m1 + n * n / 4) - n/2)\n",
    "        m1 -= (n + a) * a\n",
    "        m2 -= (n + a) * (a - 1)\n",
    "\n",
    "        n += 2 * a\n",
    "\n",
    "        if m2 >= n:\n",
    "            m2 -= n\n",
    "            n += 1\n",
    "        \n",
    "        if swap:   \n",
    "            m1, m2 = m2, m1\n",
    "\n",
    "        return [n, m1, m2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        time = 1\n",
    "        while memory1 >= time or memory2 >= time:\n",
    "            if memory2 > memory1:\n",
    "                memory2 -= time\n",
    "            else:\n",
    "                memory1 -= time\n",
    "            time += 1\n",
    "        return [time, memory1, memory2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        i = 1\n",
    "        while True:\n",
    "            if memory1 >= memory2:\n",
    "                if i > memory1:\n",
    "                    break\n",
    "                memory1 -= i\n",
    "            else:\n",
    "                if i > memory2:\n",
    "                    break\n",
    "                memory2 -= i\n",
    "            i += 1\n",
    "        return [i, memory1, memory2]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        left1 = memory1\n",
    "        left2 = memory2\n",
    "        i = 1\n",
    "        while 1:\n",
    "            if  left1 >= left2:\n",
    "                if left1 - i >= 0:\n",
    "                    left1 -= i\n",
    "                else:\n",
    "                    break\n",
    "            else:\n",
    "                if left2 - i >= 0:\n",
    "                    left2 -= i\n",
    "                else:\n",
    "                    break\n",
    "            i += 1\n",
    "        return [i, left1, left2]\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        time = 1\n",
    "        while memory1 >= time or memory2 >= time:\n",
    "            if memory2 > memory1:\n",
    "                memory2 -= time\n",
    "            else:\n",
    "                memory1 -= time\n",
    "            time += 1\n",
    "        return [time, memory1, memory2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        time = 1\n",
    "        while max(memory1, memory2) >= time:\n",
    "            if memory2 > memory1:\n",
    "                memory2 -= time\n",
    "            else:\n",
    "                memory1 -= time\n",
    "            time += 1\n",
    "        return [time, memory1, memory2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        for i in range(max(memory1,memory2)+2):\n",
    "            if(memory1>=memory2 and memory1>=i):\n",
    "                memory1-=i\n",
    "            elif(memory1<memory2 and memory2>=i):\n",
    "                memory2-=i\n",
    "            else:\n",
    "                break\n",
    "        return [i,memory1,memory2]\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        \n",
    "        cost=1\n",
    "        while memory1>=0 and memory2>=0:\n",
    "            if memory1>=memory2:\n",
    "                if memory1>=cost:\n",
    "                    memory1-=cost\n",
    "                else:\n",
    "                    break\n",
    "            else:\n",
    "                if memory2>=cost:\n",
    "                    memory2-=cost\n",
    "                else:\n",
    "                    break           \n",
    "            cost+=1\n",
    "            \n",
    "\n",
    "        return [cost,memory1,memory2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        i = 1\n",
    "        while True:\n",
    "            if memory1 >= memory2:\n",
    "                if memory1 < i:\n",
    "                    break\n",
    "                memory1 -= i\n",
    "            else:\n",
    "                if memory2 < i:\n",
    "                    break\n",
    "                memory2 -= i\n",
    "            i += 1\n",
    "        return [i, memory1, memory2]\n",
    "                    \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        time = 1\n",
    "        while True:\n",
    "            if memory1 >= memory2:\n",
    "                if memory1 >= time:\n",
    "                    memory1 -= time\n",
    "                else:\n",
    "                    break\n",
    "            else:\n",
    "                if memory2 >= time:\n",
    "                    memory2 -= time\n",
    "                else:\n",
    "                    break\n",
    "            time += 1\n",
    "        return [time, memory1, memory2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        \n",
    "        cur = 1\n",
    "        t = 1\n",
    "        while memory1 >= cur or memory2 >= cur:\n",
    "            t += 1\n",
    "            \n",
    "            if memory1 >= memory2:\n",
    "                memory1 -= cur\n",
    "            else:\n",
    "                memory2 -= cur\n",
    "            cur += 1\n",
    "        return [t,memory1,memory2]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        temp = [[-memory1, 0], [-memory2, 1]]\n",
    "        heapq.heapify(temp)\n",
    "        res = []\n",
    "        i = 1\n",
    "        while -temp[0][0] >= i:\n",
    "            num, index = heapq.heappop(temp)\n",
    "            num += i\n",
    "            heapq.heappush(temp, [num, index])\n",
    "            i += 1\n",
    "        res = [i, 0, 0]\n",
    "        for num, index in temp:\n",
    "            if index == 1:\n",
    "                res[2] = -num\n",
    "            else:\n",
    "                res[1] = -num\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        t=1\n",
    "        while t<=max(memory1,memory2):\n",
    "            if memory1<memory2:\n",
    "                memory2-=t\n",
    "            else:\n",
    "                memory1-=t\n",
    "            t+=1\n",
    "        return [t,memory1,memory2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        t = 1\n",
    "        while t <= max(memory1, memory2):   # 是否可分配\n",
    "            if memory1 < memory2:   # 分配给 2\n",
    "                memory2 -= t\n",
    "            else:   # 分配给 1\n",
    "                memory1 -= t\n",
    "            t += 1\n",
    "        return [t, memory1, memory2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # * 模拟\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        t = 1\n",
    "        while t <= max(memory1, memory2):   # 是否可分配\n",
    "            if memory1 < memory2:   # 分配给 2\n",
    "                memory2 -= t\n",
    "            else:   # 分配给 1\n",
    "                memory1 -= t\n",
    "\n",
    "            t += 1\n",
    "\n",
    "        return [t, memory1, memory2]\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        i = 1\n",
    "        while memory1 >= i or memory2 >= i:\n",
    "            if memory1 >= memory2:\n",
    "                memory1 -= i\n",
    "            else:\n",
    "                memory2 -= i\n",
    "            i += 1\n",
    "        return i, memory1, memory2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def memLeak(self, memory1: int, memory2: int) -> List[int]:\n",
    "        i=1\n",
    "        while memory2>=i or memory1>=i:\n",
    "            if memory1>=memory2 and memory1>=i:\n",
    "                memory1-=i\n",
    "            elif memory2>memory1 and memory2>=i:\n",
    "                memory2-=i \n",
    "            i+=1\n",
    "        return [i,memory1,memory2]"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
